package com.wind.trademark.client.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wind.trademark.client.dao.CompanyDao;
import com.wind.trademark.client.model.CtCompany;
import com.wind.trademark.client.model.CtContactor;
import com.wind.trademark.client.service.CompanyService;
import com.wind.trademark.common.dao.BaseDao;
import com.wind.trademark.common.exception.TmException;
import com.wind.trademark.common.vo.ComboBean;
import com.wind.trademark.common.vo.PageBean;
import com.wind.trademark.common.vo.PageParam;






@Service
public class CompanyServiceImpl implements CompanyService{
	
	@Autowired
	private CompanyDao companyDao; 
	@Autowired
	private BaseDao baseDao;

	@Override
	public CtCompany saveOrUpdateCompany(CtCompany company) {
		company.setCreateBy(1L);
		company.setCreateTime(new Date());
		company.setLastUpdateTime(new Date());
		company.setLastUpdateBy(1L);
		 baseDao.saveOrUpdate(company);
		 return company;
	} 
	
	@Override
	public void deleteCompany(Long id) {
		
		 baseDao.delete(this.getCompany(id));
	}
	
	@Override
	public PageBean queryCompany(PageParam pageParam, Map<String, Object> params) {
		return companyDao.queryCompany(pageParam,params);
	}
	 
	@Override
	public CtCompany getCompany(Long id) {
		return baseDao.get(CtCompany.class, id);
	}

	@Override
	public List<CtCompany> getCtCompanyByCompanyName(String compName) {
		return companyDao.getCtCompanyByCompanyName( compName);
	}

	@Override
	public List<ComboBean> goolgeCtCompanyByCompanyName(Map<String, Object> params) {
		return companyDao.goolgeCtCompanyByCompanyName( params);
	}

	@Override
	public PageBean queryContactor(PageParam pageParam,
			Map<String, Object> params) {
		// TODO Auto-generated method stub
		return companyDao.queryContactor(pageParam,params);
	}

	@Override
	public CtContactor saveOrUpdateContactor(CtContactor ctContactor) {
		if("Y".equals(ctContactor.getIsdefault())){
			companyDao.updateContactorUnDefault(ctContactor.getCompId());
			baseDao.getHibernateTemplate().flush();
			ctContactor.setIsdefault("Y");
		}
		baseDao.saveOrUpdate(ctContactor);
		return ctContactor;
	}

	@Override
	public void deleteContactors(List<Long> ids) {
		companyDao.deleteContactors(ids);
	}

	@Override
	public void setDefaultContactor(Long id) {
		CtContactor ctContactor=baseDao.get(CtContactor.class, id);
		Long compId=ctContactor.getCompId();
		if(!"Y".equals(ctContactor.getIsdefault())){
			companyDao.updateContactorUnDefault(compId);
			baseDao.getHibernateTemplate().flush();
			ctContactor.setIsdefault("Y");
			baseDao.update(ctContactor);
		}

	}

	@Override
	public List<CtContactor> getContactorsByCompId(Long compId) {
		return baseDao.findByProperty(CtContactor.class, "compId", compId);
	}

	@Override
	public CtContactor loadContactor(Long id) {
		return this.baseDao.get(CtContactor.class, id);
	}

	@Override
	public CtContactor getDefaultContactorsByCompId(Long compId) throws TmException {
		CtContactor ctContactor=new CtContactor();
		ctContactor.setCompId(compId);
		ctContactor.setIsdefault("Y");
		 List<CtContactor> ctContactorList=this.baseDao.findByExample(ctContactor);
		 if(ctContactorList.size()>1){
			 throw new TmException(TmException.STATUS_EXCEPTION, "数据错误,该厂家存在多个默认联系人。");
		 }
		return ctContactorList.isEmpty()?null:ctContactorList.get(0);
	}

	
} 